package com.frxuexi.mdm2206.demo

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.frxuexi.mdm2206.R
import java.text.SimpleDateFormat

data class MessageEntity(val img: Int, val title: String, val time: String,var count:Int)

@OptIn(ExperimentalLayoutApi::class)
@Preview
@Composable
fun Demo03() {
    val list = mutableListOf<MessageEntity>()
    for (i in 1..20) {
        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        list.add(MessageEntity(R.drawable.ic_launcher_background, "王五" + i, time,i))
    }



    LazyColumn(
        Modifier
            .fillMaxSize()
            .padding(20.dp),verticalArrangement = Arrangement.spacedBy(10.dp), content = {
        items(list) {
            MessageItem(it = it)
        }
    })

    /*LazyRow(
        Modifier
            .fillMaxSize()
            .padding(20.dp), horizontalArrangement = Arrangement.spacedBy(10.dp), content = {
        items(list) {
            Row {
                Image(
                    painter = painterResource(id = it.img),
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(
                            CircleShape
                        )
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = it.title)
                    Text(text = it.time, fontSize = 12.sp, color = Color.LightGray)
                }
            }
        }
    })*/

    /*LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
        items(list) {
            Row {
                Image(
                    painter = painterResource(id = it.img),
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(
                            CircleShape
                        )
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = it.title)
                    Text(text = it.time, fontSize = 12.sp, color = Color.LightGray)
                }
            }
        }
    })*/

    /* LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2), content = {
         items(list) {
             Row {
                 Image(
                     painter = painterResource(id = it.img),
                     contentDescription = "",
                     modifier = Modifier
                         .size(80.dp)
                         .clip(
                             CircleShape
                         )
                 )
                 Spacer(modifier = Modifier.width(10.dp))
                 Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                     Text(text = it.title)
                     Text(text = it.time, fontSize = 12.sp, color = Color.LightGray)
                 }
             }
         }
     })*/

    val list2 = listOf<String>(
        "Price: High to Low",
        "Avg rating: 4+",
        "Free breakfast",
        "Free cancellation",
        "£50 pn",
        "Price: High to Low",
        "Avg rating: 4+",
        "Free breakfast",
        "Free cancellation",
        "£50 pn",
    )

    /*FlowRow(
        horizontalArrangement = Arrangement.spacedBy(10.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        modifier = Modifier
            .fillMaxSize()
            .padding(8.dp)
    ) {
        list2.forEach {
            FlowItem(str = it)
        }
    }*/
}

@Composable
fun MessageItem(it: MessageEntity){
    var color by remember {
        mutableStateOf(R.color.white)
    }
    var count by remember {
        mutableStateOf(it.count)
    }
    Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween) {
        Row {
            Image(
                painter = painterResource(id = it.img),
                contentDescription = "",
                modifier = Modifier
                    .size(80.dp)
                    .clip(
                        CircleShape
                    )
            )
            Spacer(modifier = Modifier.width(10.dp))
            Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                Text(text = it.title)
                Text(text = "粉丝：${count}")
                Text(text = it.time, fontSize = 12.sp, color = Color.LightGray)
            }
        }

        Text(text = "关注", modifier = Modifier
            .background(colorResource(id = color), RoundedCornerShape(10.dp))
            .border(
                1.dp, Color.Black,
                RoundedCornerShape(10.dp)
            )
            .padding(10.dp, 5.dp)
            .clickable {
                color = R.color.orange
                count += 1
            }
        )

    }
}

@Composable
fun FlowItem(str: String) {
    Text(text = str,
        Modifier
            .background(Color.Yellow, RoundedCornerShape(10.dp))
            .border(1.dp, Color.Red, RoundedCornerShape(10.dp))
            .padding(10.dp, 3.dp)
    )
}